package controller;

import bean.Emp;
import bean.ResultCode;
import com.alibaba.fastjson.JSONArray;
import service.EmpService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/emp")
public class EmpController extends BaseController {
    private EmpService empService;

    @Override
    public void init() throws ServletException {
        empService = new EmpService();
    }

    private void find(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int empNo = Integer.parseInt(req.getParameter("id"));
        Emp emp = empService.findEmpByNo(empNo);
        ResultCode<Emp> resultCode = new ResultCode<>();
        if (emp == null) {
            resultCode.setCode("401");
            resultCode.setMessage("该员工不存在");
        } else {
            resultCode.setCode("200");
            resultCode.setData(emp);
        }
        String json = JSONArray.toJSONString(resultCode);
        PrintWriter out = resp.getWriter();
        out.print(json);
    }
}
